package product;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.SystemColor;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;
import javax.swing.plaf.ComboBoxUI;
import javax.swing.plaf.basic.BasicArrowButton;
import javax.swing.plaf.basic.BasicComboBoxUI;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

import net.sourceforge.jdatepicker.impl.JDatePanelImpl;
import net.sourceforge.jdatepicker.impl.JDatePickerImpl;
import net.sourceforge.jdatepicker.impl.UtilDateModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SpringLayout;

public class TotalList extends JInternalFrame {
	ProductMain4 pm;

	DefaultTableModel model = null;
	DefaultListModel listModel = null;
	
	MyTableRender render;
	TableRowSorter<TableModel> tSoter;
		
	SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
	DecimalFormat df = new DecimalFormat("###,###,###,###,### 원");
	DecimalFormat de = new DecimalFormat("###,###,###,###,### 개");

	int count = 0;
	boolean flag = false;
	
	String find;
	String sql = null;
	
	private JScrollPane scrollPane;
	private JTable table;
	private JScrollPane scrollPane_1;
	private JList list;
	private JComboBox comboBox;
	private JTextField inputData;
	private JPanel panel;
	private JPanel panel_1;
	private JLabel lblSearch;
	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;
	private JDatePickerImpl datePicker;
	private JLabel lbl;
	private SpringLayout springLayout;

	public TotalList(ProductMain4 pm) {
		super("Total List Search", false,true,false,true);
		setBackground(SystemColor.menu);
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		getContentPane().setBackground(Color.WHITE);

		addInternalFrameListener(new InternalFrameAdapter() {
			
			// internalFrame이 닫을 경우 JTable 초기화 
			public void internalFrameClosing(InternalFrameEvent e) {
				inputData.setText("");
				if(model != null){
					for(int i = model.getRowCount() - 1; i >= 0 ; i--)
						model.removeRow(i);
				}
			}
		});
		setBounds(450, 0, 640, 630);
		getContentPane().setLayout(null);
		getContentPane().add(getScrollPane_1());
		getContentPane().add(getScrollPane());
		getContentPane().add(getPanel());
		getContentPane().add(getPanel_1());
		
		this.pm = pm;
		
		datePicker.setVisible(false);
	}
	public void searchData(){
		find = inputData.getText();
		try{
			searchSql();
			PreparedStatement pstmt = null;
			if(find.equals("")){
				if(comboBox.getSelectedIndex() == 0){
					sql = sql + " order by 사업자번호";
					
				}else if(comboBox.getSelectedIndex() == 1){
					sql = sql + " order by 생산번호";

				}else if(comboBox.getSelectedIndex() == 2){
					sql = sql + " order by 납품번호";
					
				}else if(comboBox.getSelectedIndex() == 3){
					sql = sql + " order by 반품번호";
					
				}else if(comboBox.getSelectedIndex() == 4){
					sql = sql + " order by 제품코드";
				}
				pstmt = pm.conn.prepareStatement(sql);
				
			}else {
				if(comboBox.getSelectedIndex() == 0){
					sql = sql + " where num like ? or name like ? or ceo like ?  order by 사업자번호";
					
				}else if(comboBox.getSelectedIndex() == 1){
					sql = sql + " where to_char(p.nal, 'yyyy-mm-dd') like ? or p.code like ? or c.pro_nm like ? order by 생산번호";

				}else if(comboBox.getSelectedIndex() == 2){
					sql = sql + " where to_char(d.nal, 'yyyy-mm-dd') like ? or d.code like ? or c.pro_nm like ? order by 납품번호";
					
				}else if(comboBox.getSelectedIndex() == 3){
					sql = sql + " where to_char(d.nal, 'yyyy-mm-dd') like ? or d.code like ? or c.pro_nm like ? order by 반품번호";
					
				}else if(comboBox.getSelectedIndex() == 4){
					sql = sql + " where d.code like ? or c.pro_nm like ? or d.ea like ? order by 제품코드";
				}
				
				pstmt = pm.conn.prepareStatement(sql);
				pstmt.setString(1, "%"+find+"%");
				pstmt.setString(2, "%"+find+"%");
				pstmt.setString(3, "%"+find+"%");
			}

			if(pstmt.executeUpdate() > 0){
				ResultSet rs = pstmt.executeQuery();
				ResultSetMetaData rsmd = rs.getMetaData();
				columnSetting(rs);
				
			    int count = 1;
				while(rs.next()){
					Vector vec = new Vector();
					vec.add(count);
					
					for(int i = 1; i <= rsmd.getColumnCount(); i++){
						vec.add(rs.getString(i));
		            }
					
					model.addRow(vec);
					count++;
				}
				
				rs.close();
			}else{
				JOptionPane.showMessageDialog(null, "일치하는 값이 없습니다. 다시 조회하시오.");
				table.setModel(new DefaultTableModel());
			}
			pstmt.close();
			
		}catch(Exception ex){
			ex.getMessage();
			System.out.println("에러!!");
		}
		
		scrollPane_1.setBounds(159, 140, 322, 0);
		scrollPane_1.setVisible(false);
		list.setVisible(false);
	}
	public void searchSql(){
		try{
			if(comboBox.getSelectedIndex() == 0){
				sql = "select num as 사업자번호, name as 회사명, ceo as 대표자, address as 주소, phone as 연락처, fax as 팩스 " 
					+ "from customer";
				
			}else if(comboBox.getSelectedIndex() == 1){
				sql = "select p.num as 생산번호, p.code as 제품코드, c.pro_nm as 제품이름, to_char(c.pro_price, '999,999,999') as 원가, " 
					+ "to_char(p.ea, '999,999,999') as 수량, p.irum as 생산담당자, to_char(p.nal, 'yyyy-mm-dd') as 생산일자 "
					+ "from (select p.*, m.* from product p inner join memberdata m on p.member_id = m.memberid) p inner join code c " 
					+ "on p.code = c.pro_code";

			}else if(comboBox.getSelectedIndex() == 2){
				sql = "select d.num as 납품번호, d.code as 제품코드, c.pro_nm as 제품이름, to_char(c.pro_price, '999,999,999') as 원가, "
					+ "to_char(d.ea, '999,999,999') as 수량, d.irum as 납품담당자, to_char(d.nal, 'yyyy-mm-dd') as 납품일자 "
					+ "from (select p.*, m.* from delivery p inner join memberdata m on p.member_id = m.memberid) d inner join code c "
					+ "on d.code = c.pro_code ";
				
			}else if(comboBox.getSelectedIndex() == 3){
				sql = "select d.num as 반품번호, d.code as 제품코드, c.pro_nm as 제품이름, to_char(c.pro_price, '999,999,999') as 원가, "
					+ "to_char(d.ea, '999,999,999') as 수량, d.irum as 반품담당자, to_char(d.nal, 'yyyy-mm-dd') as 반품일자 "
					+ "from (select p.*, m.* from delivery_recall p inner join memberdata m on p.member_id = m.memberid) d inner join code c "
					+ "on d.code = c.pro_code ";

			}else if(comboBox.getSelectedIndex() == 4){
				sql = "select d.code as 제품코드, c.pro_nm as 제품명, to_char(d.ea, '999,999,999') as 재고량 " 
					+ "from delivery_stock d inner join code c "
					+ "on d.code = c.pro_code ";
			}
		}catch(Exception ex){ex.getStackTrace();}
	}
	
	// 테이블 컴럼 지정
	public void columnSetting(ResultSet rs){
		try{
			ResultSetMetaData rsmd = rs.getMetaData();
			
			String[] column = new String[rsmd.getColumnCount() + 1];
			column[0] = "순번";
		    for(int i = 1; i <= rsmd.getColumnCount(); i++){
		    	column[i] = rsmd.getColumnName(i);
		    }
		    
		    model = new DefaultTableModel(column, 0){
				// 테이블의 값을 변경하지 못하도록 설정
				public boolean isCellEditable(int i, int c){
					return false;
				}
			};
			table.setModel(model);
			
			tableSetting();
			
		}catch(Exception ex){ex.getStackTrace();}
	}
	
	// suggest 기능 구현할때 사용하는 쿼리
	public void autoSearchSql(){
		try{
			if(comboBox.getSelectedIndex() == 0){
				sql = "select distinct name as 회사명, ceo as 대표자 " 
					+ "from customer";
				
			}else if(comboBox.getSelectedIndex() == 1){
				sql = "select distinct p.code as 제품코드, c.pro_nm as 제품이름 "
					+ "from product p inner join code c " 
					+ "on p.code = c.pro_code";
				
			}else if(comboBox.getSelectedIndex() == 2){
				sql = "select distinct d.code as 제품코드, c.pro_nm as 제품이름 "
					+ "from delivery d inner join code c "
					+ "on d.code = c.pro_code ";
				
			}else if(comboBox.getSelectedIndex() == 3){
				sql = "select distinct d.code as 제품코드, c.pro_nm as 제품이름 "
					+ "from delivery_recall d inner join code c "
					+ "on d.code = c.pro_code ";

			}else if(comboBox.getSelectedIndex() == 4){
				sql = "select distinct d.code as 제품코드, c.pro_nm as 제품명 " 
					+ "from delivery_stock d inner join code c "
					+ "on d.code = c.pro_code ";
			}
		}catch(Exception ex){ex.getStackTrace();}
	}
	
	// suggest 기능
	public void autoSearch(){
		try{	
			listModel = new DefaultListModel();
			count = 0;
			String finds = inputData.getText();
			
			autoSearchSql();
			PreparedStatement pstmt = pm.conn.prepareStatement(sql);
			
			if(pstmt.executeUpdate() > 0){
				ResultSet rs = pstmt.executeQuery();
				while(rs.next()){
					if(rs.getString(1).contains(finds)){
						listModel.addElement(colorText(rs.getString(1), finds));
						count++;
					}
					if(rs.getString(2).contains(finds)){
						listModel.addElement(colorText(rs.getString(2), finds));
						count++;
					}
				}

				if(count == 0){
					scrollPane_1.setVisible(false);
					list.setVisible(false);
				}else{
					scrollPane_1.setBounds(141, 140, 451, (count * 18) + 7);
					list.setModel(listModel);
					scrollPane_1.setVisible(true);
					list.setVisible(true);
				}
				rs.close();
			}
			pstmt.close();
		}catch(Exception ex){
			ex.getStackTrace();
			System.out.println("auto search error!!");
		}
	}
	
	public String colorText(String base, String find){
		String str = "<html><font color = #000000>" + base + "</font></html>";
		String re = "</font><font color = #0880fb>" + find + "</font><font color = #000000>";
		str = str.replace(find, re);
		return str;
	}
	public String replaceText(String base){
		base = base.replaceAll("<html>", "");
		base = base.replaceAll("</html>", "");
		base = base.replaceAll("<font color = #000000>", "");
		base = base.replaceAll("<font color = #0880fb>", "");
		base = base.replaceAll("</font>", "");
		return base;
	}
	
	// 테이블 간격 및 정렬 지정
	public void tableSetting(){
		if(comboBox.getSelectedIndex() == 0){
			char[] c = {'c', 'c', 'c', 'c', 'c', 'c', 'c'};
			int[] w = {30, 80, 100, 50, 80, 80, 80};
			render = new MyTableRender(table, c, w);
			
		}else if(comboBox.getSelectedIndex() == 1){
			char[] c = {'c', 'c', 'c', 'c', 'r', 'r', 'c', 'c'};
			int[] w = {30, 35, 40, 50, 50, 50, 50, 50};
			render = new MyTableRender(table, c, w);
			
		}else if(comboBox.getSelectedIndex() == 2){
			char[] c = {'c', 'c', 'c', 'c', 'r', 'r', 'c', 'c'};
			int[] w = {30, 35, 40, 50, 50, 50, 50, 50};
			render = new MyTableRender(table, c, w);
			
		}else if(comboBox.getSelectedIndex() == 3){
			char[] c = {'c', 'c', 'c', 'c', 'r', 'r', 'c', 'c'};
			int[] w = {30, 35, 40, 50, 50, 50, 50, 50};
			render = new MyTableRender(table, c, w);
			
		}else if(comboBox.getSelectedIndex() == 4){
			char[] c = {'c', 'c', 'c', 'r'};
			int[] w = {30, 80, 100, 50};
			render = new MyTableRender(table, c, w);
		}
		tSoter = new TableRowSorter(model);
		table.setRowSorter(tSoter);
	}
	
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setAutoscrolls(true);
			scrollPane.setBackground(Color.WHITE);
			scrollPane.getViewport().setBackground(Color.white);
			scrollPane.setBorder(new LineBorder(new Color(128, 128, 128)));
			scrollPane.setBounds(24, 170, 580, 400);
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}
	public JTable getTable() {
		if (table == null) {
			
			UIManager.put("Table.background", Color.white);
			UIManager.put("Table.foreground", Color.black);
			UIManager.put("Table.selectionBackground", new Color(0x0880fb));
			UIManager.put("Table.selectionForeground", Color.white);
			
			table = new JTable();	
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					
					// 더블클릭 했을 경우 Update창 실행
					if(e.getClickCount() == 2){
						
						// 테이블의 선택한 row 값 저장
						int row = table.getSelectedRow();
						
						// 콤보박스의 선택된 값을 찾아 업데이트창 실행
						// 거래처
						if(comboBox.getSelectedIndex() == 0){
							pm.openCompanyUpdate();
							pm.cpUpdate.txtNum.setText(table.getValueAt(row, 1).toString());
							pm.cpUpdate.search();
							
						// 생산
						}else if(comboBox.getSelectedIndex() == 1){
							pm.openProductUpdate();
							pm.pUpdate.search.setText(table.getValueAt(row, 1).toString());
							pm.pUpdate.search();

							
						// 납품
						}else if(comboBox.getSelectedIndex() == 2){
							pm.openDeliveryUpdate();
							pm.dUpdate.inputDeliverynum.setText(table.getValueAt(row, 1).toString());
							pm.dUpdate.searchDB();

						
						// 반품
						}else if(comboBox.getSelectedIndex() == 3){
							pm.OpenDeliveryRecallUpdate();
							pm.dru.textRecallNum.setText(table.getValueAt(row, 1).toString());
							pm.dru.drNumSearch();
							pm.dru.setVisible(true);
							
							
						// 납품재고
						}else if(comboBox.getSelectedIndex() == 4){

						}
					}
				}
			});
			DefaultTableCellRenderer renderer = (DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer();
	        renderer.setHorizontalAlignment(0);
			
		}
		return table;
	}
	private JScrollPane getScrollPane_1() {
		if (scrollPane_1 == null) {
			scrollPane_1 = new JScrollPane();
			scrollPane_1.setBorder(new LineBorder(new Color(0x0880fb), 2));
			scrollPane_1.setViewportView(getList());
		}
		return scrollPane_1;
	}
	private JList getList() {
		if (list == null) {
			
			UIManager.put("List.selectionBackground", new Color(0x0880fb));
			UIManager.put("List.selectionForeground", Color.white);
			
			list = new JList();
			list.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					// 클릭 했을 경우 
					if(e.getClickCount() == 1){
						inputData.setText(replaceText(list.getSelectedValue().toString()));
						searchData();
					}
				}
			});
			list.addKeyListener(new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent e) {

					if(e.getKeyCode() == KeyEvent.VK_ENTER){
						inputData.setText(replaceText(list.getSelectedValue().toString()));
						searchData();
					}
				}
			});
			list.setBorder(new LineBorder(new Color(255,255,255), 2));
			list.setVisible(true);
		}
		return list;
	}
	public JComboBox getComboBox_1() {
		if (comboBox == null) {
			
			UIManager.put("ComboBox.background", Color.white);
			UIManager.put("ComboBox.foreground", Color.black);
			UIManager.put("ComboBox.selectionBackground", new Color(0x0880fb));
			UIManager.put("ComboBox.selectionForeground", Color.white);
			
			comboBox = new JComboBox();			
			comboBox.setBounds(15, 10, 104, 30);
			comboBox.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					// 텍스트 필드 초기화
					inputData.setText("");
					
					// 테이블 초기화
					if(model != null)
						model.setNumRows(0);

					searchData();
					
					if(comboBox.getSelectedItem() == "생산" || comboBox.getSelectedItem() == "납품" || comboBox.getSelectedItem() == "반품"){
						datePicker.setVisible(true);
						lbl.setVisible(false);
					}else{
						datePicker.setVisible(false);
						lbl.setVisible(true);
					}
						
				}
			});
			comboBox.setUI(ColorArrowUI.createUI(comboBox));
			comboBox.setBorder(null);
			comboBox.setBackground(Color.WHITE);
			comboBox.setModel(new DefaultComboBoxModel(new String[] {"거래처", "생산", "납품", "반품", "납품재고"}));
		}
		return comboBox;
	}
	private JTextField getInputData() {
		if (inputData == null) {
			inputData = new JTextField();
			inputData.setBounds(135, 10, 357, 30);
			inputData.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent e) {
					if(e.getKeyCode() == KeyEvent.VK_DOWN){
						if(list != null)
							if(list.isSelectionEmpty())
								list.setSelectedIndex(0);
							else
								list.setSelectedIndex(list.getSelectedIndex()+1);
						
					}else if(e.getKeyCode() == KeyEvent.VK_UP){
						if(list != null)
							if(!list.isSelectionEmpty())
								list.setSelectedIndex(list.getSelectedIndex()-1);
						
					}else if(e.getKeyCode() != KeyEvent.VK_ENTER){
						if(!inputData.getText().equals("")){
							autoSearch();
						}
					}
				}
				public void keyPressed(KeyEvent e) {
					if(e.getKeyCode() == KeyEvent.VK_ENTER){
						if(list != null)
							if(!list.isSelectionEmpty())
								inputData.setText(replaceText(list.getSelectedValue().toString()));
						searchData();
					}
				}
			});
			inputData.setColumns(10);
			inputData.setBorder(null);
		}
		return inputData;
	}
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setBackground(SystemColor.activeCaption);
			panel.setBounds(24, 100, 580, 50);
			panel.setLayout(null);
			panel.add(getLabel_4());
			panel.add(getDatePicker());			
			panel.add(getComboBox_1());
			panel.add(getInputData());
			panel.add(getLabel_2());
			panel.add(getLabel_3());
		}
		return panel;
	}
	private JDatePickerImpl getDatePicker(){
		if (datePicker == null) {
			UtilDateModel uModel = new UtilDateModel();
			JDatePanelImpl datePanel = new JDatePanelImpl(uModel);
			datePicker = new JDatePickerImpl(datePanel);
			datePicker.setBorder(null);
			springLayout = (SpringLayout) datePicker.getLayout();
			
			datePicker.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					inputData.setText(sd.format((Date)datePicker.getModel().getValue()));
					searchData();
				}
			});
			datePicker.setBackground(Color.WHITE);
			datePicker.getJFormattedTextField().setVisible(false);
			datePicker.setSize(45, 30);
			datePicker.setLocation(492, 10);
			
		}
		return datePicker;
	}
	
	private JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.setLayout(null);
			panel_1.setBorder(new LineBorder(Color.GRAY, 2));
			panel_1.setBounds(-21, -21, 673, 104);
			panel_1.add(getLabel_1());
		}
		return panel_1;
	}
	private JLabel getLabel_1() {
		if (lblSearch == null) {
			lblSearch = new JLabel("Total List Search");
			lblSearch.setHorizontalAlignment(SwingConstants.CENTER);
			lblSearch.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 40));
			lblSearch.setBounds(165, 31, 325, 61);
		}
		return lblSearch;
	}
	private JLabel getLabel_2() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel(new ImageIcon("img\\search.png"));
			lblNewLabel.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					searchData();
				}
			});
			lblNewLabel.setBounds(537, 10, 30, 30);
		}
		return lblNewLabel;
	}
	private JLabel getLabel_3() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("");
			lblNewLabel_1.setOpaque(true);
			lblNewLabel_1.setBackground(Color.WHITE);
			lblNewLabel_1.setBorder(null);
			lblNewLabel_1.setBounds(117, 10, 20, 30);
		}
		return lblNewLabel_1;
	}
	private JLabel getLabel_4() {
		if (lbl == null) {
			lbl = new JLabel("");
			lbl.setBackground(Color.WHITE);
			lbl.setOpaque(true);
			lbl.setBounds(492, 10, 45, 30);
		}
		return lbl;
	}
}
class ColorArrowUI extends BasicComboBoxUI {

    public static ComboBoxUI createUI(JComponent c) {
        return new ColorArrowUI();
    }

    @Override 
    protected JButton createArrowButton() {
        return new BasicArrowButton(BasicArrowButton.SOUTH, Color.white, Color.GRAY, Color.GRAY, Color.white);
    }
}
class DepthButton extends JButton{
	public DepthButton(String text){
		super(text);
		// 스윙이 배경을 그리지 않게 함
		setContentAreaFilled(false);
	}
	
	@Override
	protected void paintComponent(Graphics g){
		Graphics2D g2 = (Graphics2D) g;
		
		// 이중 그래디언트 생성
		GradientPaint p;
		p = new GradientPaint(0, getHeight(), new Color(0xffffff), 0, 0, new Color(0xffffff));
	 	
		// 상태 보존
		Paint oldpPaint = g2.getPaint();
		
		// 배경 칠하기
		g2.setPaint(p);
		g2.fillRect(0, 0, getWidth(), getHeight());
		
		// 상태 복구
		g2.setPaint(oldpPaint);
		
		// 테두리, 텍스트 등 그리기
		super.paintComponent(g);
	}
}
class MyTableRender  {
	JTable table;
	char[] align;// l=left, c=center, r=right;
	int[] width;
	public MyTableRender(JTable t, char[] c, int[] w){
		DefaultTableCellRenderer render;
		this.table = t;
		this.align = c;
		this.width = w;
		
		TableColumnModel model = table.getColumnModel();
		for(int i=0; i<align.length ; i++){
			render = new DefaultTableCellRenderer();
			switch(align[i]){
			case 'l':
				render.setHorizontalAlignment(JLabel.LEFT);
				model.getColumn(i).setCellRenderer(render);
				model.getColumn(i).setPreferredWidth(width[i]);
				break;
			case 'c':
				render.setHorizontalAlignment(JLabel.CENTER);
				model.getColumn(i).setCellRenderer(render);
				model.getColumn(i).setPreferredWidth(width[i]);
				break;
			case 'r':
				render.setHorizontalAlignment(JLabel.RIGHT);
				model.getColumn(i).setCellRenderer(render);
				model.getColumn(i).setPreferredWidth(width[i]);
				break;
			}
		}
	}

}
